package com.example.redcultural;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class NewAdapter extends ArrayAdapter<NewContent> {

    private LayoutInflater layoutInflater;
    private Context context;

    private ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() + 1,
            Runtime.getRuntime().availableProcessors() * 2 + 1,
            0,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(),
            new ThreadFactory() {
                private AtomicInteger atomicInteger=new AtomicInteger(1);
                @Override
                public Thread newThread(Runnable r) {
                    return new Thread(r,"Thread_"+atomicInteger.getAndIncrement());
                }
            });
    public NewAdapter(Context context, List<NewContent> NewContents) {
        super(context, R.layout.itemnews, R.id.new_title, NewContents);
        layoutInflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view;
        final ViewHolder viewHolder;
        final NewContent newContent = getItem(position);
        if (convertView == null) {
            view = layoutInflater.inflate(R.layout.itemnews, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.title = view.findViewById(R.id.new_title);
            viewHolder.im1 = view.findViewById(R.id.new_image_1);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.title.setText(newContent.getTitle());

        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                final Bitmap bmp1 = getURLimage(newContent.getImg());
                final Bitmap bmp2 = getURLimage(newContent.getImg2());
                ((AppCompatActivity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        viewHolder.im1.setImageBitmap(bmp1);
                    }
                });
            }
        });
        return view;
    }

    class ViewHolder {
        TextView title;
        ImageView im1;

    }
    private Bitmap getURLimage(String imageUrl) {
        Bitmap bmp = null;
        try {
            URL myurl = new URL(imageUrl);
            // 获得连接
            HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
            conn.setConnectTimeout(10000);//设置超时
            conn.setDoInput(true);
            conn.setUseCaches(false);//不缓存
            conn.connect();
            InputStream is = conn.getInputStream();//获得图片的数据流
            bmp = BitmapFactory.decodeStream(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bmp;
    }

}
